unit Class_UNIT;

interface
uses
  Classes,SysUtils,DB,Uni,UniEngine;

type
  TUNIT=class(TUniEngine)
  private
    FUNITLINK : string;
    FUNITCODE : string;
    FUNITNAME : string;
    FUNITMEMO : string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;
  published
    property UNITLINK : string read FUNITLINK  write FUNITLINK;
    property UNITCODE : string read FUNITCODE  write FUNITCODE;
    property UNITNAMF : string read FUNITNAME  write FUNITNAME;
    property UNITMEMO : string read FUNITMEMO  write FUNITMEMO;
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(AUNIT:TUNIT):TUNIT;overload;
    class procedure CopyIt(AUNIT:TUNIT;var Result:TUNIT);overload;
  end;

implementation

{ TUNIT }
procedure TUNIT.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('UNIT_CODE').Value := UNITCODE;
        ParamByName('UNIT_NAME').Value := UNITNAMF;
        ParamByName('UNIT_MEMO').Value := UNITMEMO;
      end;
      otEdit:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('UNIT_CODE').Value := UNITCODE;
        ParamByName('UNIT_NAME').Value := UNITNAMF;
        ParamByName('UNIT_MEMO').Value := UNITMEMO;
      end;
      otDelt:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;

      end;
    end;
  end;
end;

function TUNIT.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_UNIT',['UNIT_LINK',UNITLINK],AUniConnection);
end;

function TUNIT.GetNextIdex: Integer;
begin

end;

function TUNIT.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TUNIT.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_UNIT WHERE   UNIT_LINK=:UNIT_LINK';
end;

function TUNIT.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_UNIT'
         +'    ( UNIT_LINK, UNIT_CODE, UNIT_NAME, UNIT_MEMO)'
         +'    VALUES'
         +'    (:UNIT_LINK,:UNIT_CODE,:UNIT_NAME,:UNIT_MEMO)';
end;

function TUNIT.GetStrsIndex: string;
begin
  Result:=Format('%S',[UNITLINK]);
end;

function TUNIT.GetStrUpdate: string;
begin
  Result:='UPDATE  TBL_UNIT SET'
         +'    UNIT_CODE=:UNIT_CODE,'
         +'    UNIT_NAME=:UNIT_NAME,'
         +'    UNIT_MEMO=:UNIT_MEMO'
         +'    WHERE UNIT_LINK=:UNIT_LINK';
end;

constructor TUNIT.Create;
begin

end;

destructor TUNIT.Destroy;
begin

  inherited;
end;

class function TUNIT.ReadDS(AUniQuery: TUniQuery): TUniEngine;
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  Result:=TUNIT.Create;

  with TUNIT(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=UpperCase(Field.FieldName);
      if FieldName='UNIT_LINK' then
      begin
        UNITLINK :=Field.AsString;
      end else
      if FieldName='UNIT_CODE' then
      begin
        UNITCODE :=Field.AsString;
      end else
      if FieldName='UNIT_NAME' then
      begin
        UNITNAMF :=Field.AsString;
      end else
      if FieldName='UNIT_MEMO' then
      begin
        UNITMEMO :=Field.AsString;
      end;
    end
  end;
end;

class procedure TUNIT.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  if Result=nil then Exit;

  with TUNIT(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=UpperCase(Field.FieldName);
      if FieldName='UNIT_LINK' then
      begin
        UNITLINK :=Field.AsString;
      end else
      if FieldName='UNIT_CODE' then
      begin
        UNITCODE :=Field.AsString;
      end else
      if FieldName='UNIT_NAME' then
      begin
        UNITNAMF :=Field.AsString;
      end else
      if FieldName='UNIT_MEMO' then
      begin
        UNITMEMO :=Field.AsString;
      end;
    end
  end;
end;

class function  TUNIT.CopyIt(AUNIT: TUNIT): TUNIT;
begin
  Result:=TUNIT.Create;
  TUNIT.CopyIt(AUNIT,Result)
end;

class procedure TUNIT.CopyIt(AUNIT:TUNIT;var Result:TUNIT);
begin
  if Result=nil then Exit;
  Result.UNITLINK :=AUNIT.UNITLINK;
  Result.UNITCODE :=AUNIT.UNITCODE;
  Result.UNITNAMF :=AUNIT.UNITNAMF;
  Result.UNITMEMO :=AUNIT.UNITMEMO;
end;

end.
